#!/bin/bash
clear
if [[ "$EUID" -ne 0 ]]; then
    echo -e "\033[1;31mScript need to be run as root!\033[0m"; exit 1
fi

alamat_hos=$(cat /etc/environment | grep -w 'DOMAIN' | cut -d '=' -f 2)
alamat_emel=$(cat /etc/environment | grep -w 'EMAIL' | cut -d '=' -f 2)

apt-get -qq update
apt-get install nginx -y

mkdir /var/www/html/$alamat_hos
mkdir /var/www/html/v2ray.$alamat_hos
mkdir /var/www/html/xray.$alamat_hos

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
cat > /etc/nginx/conf.d/cloudflare.conf <<-EOF
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;

set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;

real_ip_header CF-Connecting-IP;
EOF

echo '<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Cybertize Devel</title>
    </head>
    <body>
        <main class="main">
            <section class="section">
                <article>
                    <h3 class="article__title">
                        <a href="/" class="title__link">CYBERTIZE</a>
                    </h3>
                    <p class="article_body">
                        Use my referral link https://m.do.co/c/a28a40414d6a
                        to gets $100 credit into your DigitalOcean account.
                        Created by Doctype, Powered by Cybertize
                        Copyright 2021, Allright reserved.
                    </p>
                    <p class="article__footer">
                        Copyright &#169; 2021 Cybertize, All right reserved.
                    </p>
                </article>
            </section>
        </main>
    </body>
</html>' > /var/www/html/$alamat_hos/index.html

echo '<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Cybertize Devel</title>
    </head>
    <body>
        <main class="main">
            <section class="section">
                <article>
                    <h3 class="article__title">
                        <a href="/" class="title__link">CYBERTIZE</a>
                    </h3>
                    <p class="article_body">
                        Use my referral link https://m.do.co/c/a28a40414d6a
                        to gets $100 credit into your DigitalOcean account.
                        Created by Doctype, Powered by Cybertize
                        Copyright 2021, Allright reserved.
                    </p>
                    <p class="article__footer">
                        Copyright &#169; 2021 Cybertize, All right reserved.
                    </p>
                </article>
            </section>
        </main>
    </body>
</html>' > /var/www/html/v2ray.$alamat_hos/index.html

echo '<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Cybertize Devel</title>
    </head>
    <body>
        <main class="main">
            <section class="section">
                <article>
                    <h3 class="article__title">
                        <a href="/" class="title__link">CYBERTIZE</a>
                    </h3>
                    <p class="article_body">
                        Use my referral link https://m.do.co/c/a28a40414d6a
                        to gets $100 credit into your DigitalOcean account.
                        Created by Doctype, Powered by Cybertize
                        Copyright 2021, Allright reserved.
                    </p>
                    <p class="article__footer">
                        Copyright &#169; 2021 Cybertize, All right reserved.
                    </p>
                </article>
            </section>
        </main>
    </body>
</html>' > /var/www/html/xray.$alamat_hos/index.html

chown -R www-data:www-data /var/www/html/$alamat_hos
chown -R www-data:www-data /var/www/html/v2ray.$alamat_hos
chown -R www-data:www-data /var/www/html/xray.$alamat_hos

echo "server {
    listen 80;
    listen [::]:80;
    root /var/www/html/$alamat_hos;
    index index.html index.htm;
    server_name $alamat_hos;

    location / {
        try_files \$uri \$uri/ =404;
    }
}" > /etc/nginx/sites-available/$alamat_hos.conf

echo "server {
    listen 80;
    listen [::]:80;
    root /var/www/html/v2ray.$alamat_hos;
    index index.html index.htm;
    server_name v2ray.$alamat_hos;

    location / {
        try_files \$uri \$uri/ =404;
    }
}" > /etc/nginx/sites-available/v2ray.$alamat_hos.conf

echo "server {
    listen 80;
    listen [::]:80;
    root /var/www/html/xray.$alamat_hos;
    index index.html index.htm;
    server_name xray.$alamat_hos;

    location / {
        try_files \$uri \$uri/ =404;
    }
}" > /etc/nginx/sites-available/xray.$alamat_hos.conf

ln -s /etc/nginx/sites-available/$alamat_hos.conf /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/v2ray.$alamat_hos.conf /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/xray.$alamat_hos.conf /etc/nginx/sites-enabled/

apt-get -y -qq install certbot
apt-get -y -qq install python3-certbot
apt-get -y -qq install python3-certbot-nginx

certbot --non-interactive --agree-tos --email $alamat_emel --nginx -d $alamat_hos -d www.$alamat_hos
certbot --non-interactive --agree-tos --email $alamat_emel --nginx -d v2ray.$alamat_hos
certbot --non-interactive --agree-tos --email $alamat_emel --nginx -d xray.$alamat_hos

echo '10 11 * * *   root /usr/bin/certbot renew >/dev/null 2>&1' >> /etc/crontab

nginx -t && systemctl restart nginx

echo
echo -e "\033[1;32mCongratulation, We are done with the nginx installation.\033[0m"
echo
echo 'Use my referral link https://m.do.co/c/a28a40414d6a'
echo 'to gets $100 credit into your DigitalOcean account.'
echo 'Created by Doctype, Powered by Cybertize'
echo 'Copyright 2021, Allright reserved.'
echo; sleep 5
